﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.dataAcess.produceDataSetTableAdapters;
using dhDataAcess.dataAcess.ViewDataSetTableAdapters ;
using baseSet.importExport;
using baseSet;
using dhsystem.orderForm;
using dhDataAcess.dataAcess;
using dhsystem.produceForm.picking;
using dhSystemBLL.produceBLL;
using FastReport;
using dhDataAcess.baseBLL;

namespace dhsystem.produceForm
{
    public partial class FrmMrp : Form
    {
        public FrmMrp()
        {
            InitializeComponent();
        }
        productblBLL productbl = new productblBLL(); 
        //打开窗口
        private void FrmMrp_Load(object sender, EventArgs e)
        {
           

            V_orderDetailMrpTableAdapter detailMrpAdapter = new V_orderDetailMrpTableAdapter();
            bSourceDetailMrp.DataSource = detailMrpAdapter.GetDataV_orderDetailMrp ();

            dGridDetaiMrp.DataSource = bSourceDetailMrp;
            DcolumnSet.colSet(moduleInfo.V_orderDetailMrp, dGridDetaiMrp);


         


            mrpCountTableAdapter mrpCountAdapter = new mrpCountTableAdapter();
            produceDataSet.mrpCountDataTable countTable = mrpCountAdapter.GetDatamrpCount("审核", "");
            purchase.rlckDetailDataTable rlckTable = new purchase.rlckDetailDataTable();
            bSourceMrpCount.DataSource = countTable;
            dGridMrpCount.DataSource = bSourceMrpCount;
            DcolumnSet.colSet(moduleInfo.mrpCount, dGridMrpCount);
            var a = from c in countTable  
                    group c by new { c.matNum,c.rlckqty } into g
                    select new
                    {
                        
                        原材料名 = g.Key.matNum,
                      //  原材料库存 = g.Key.stockQty1,
                        已领料 = g.Key.rlckqty,
                        未领料=g.Sum (c=>c.Column1)-g.Key .rlckqty ,
                        所需罗数=g.Sum (c=> c.grCount),
                        所需码数 = g.Sum(c => c.yCount),
                        所需米数 = g.Sum(c => c.mCount),
                        百米前公斤=g.Sum (c=>c.Column2),
                        百米后公斤=g.Sum (c=>c.Column1)
                        

                        
                    };
            bSourceDetailCount.DataSource = a.ToList();
            dGridDetailCount.DataSource = bSourceDetailCount;
            dGridDetaiMrp.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dGridDetaiMrp.CellMouseUp += new DataGridViewCellMouseEventHandler(dGridDetaiMrp_CellMouseUp);
            this.dGridDetaiMrp.EditMode = DataGridViewEditMode.EditOnEnter;

            V_MrpTableAdapter V_MrpAdapter = new V_MrpTableAdapter();
            bgSourceMRP.DataSource = V_MrpAdapter.GetDataV_MrpALL();

            dGridMRP.DataSource = bgSourceMRP;
            DcolumnSet.colSet(moduleInfo.V_Mrp, dGridMRP);

           
        }
        //单个订单按钮
        private void button1_Click(object sender, EventArgs e)
        {
           // MessageBox.Show(dGridMrpCount.CurrentRow.Cells["orderDetailNo"].Value.ToString());
            mrpCountTableAdapter mrpCountAdapter = new mrpCountTableAdapter();
            dhDataAcess.dataAcess.produceDataSet.mrpCountDataTable countTable = mrpCountAdapter.GetDatamrpCount("", dGridDetaiMrp.CurrentRow.Cells["orderNo"].Value.ToString());
            bSourceMrpCount.DataSource = countTable;
            dGridMrpCount.DataSource = bSourceMrpCount;


            var a = from c in countTable
                    group c by new { c.matNum, c.stockQty1, c.rlckqty } into g
                    select new
                    {

                        原材料名 = g.Key.matNum,
                        原材料库存 = g.Key.stockQty1,
                        已领料 = g.Key.rlckqty,
                        未领料 = g.Sum(c => c.Column1) - g.Key.rlckqty,
                        所需罗数 = g.Sum(c => c.grCount),
                        所需码数 = g.Sum(c => c.yCount),
                        所需米数 = g.Sum(c => c.mCount),
                        百米前公斤 = g.Sum(c => c.Column2),
                        百米后公斤 = g.Sum(c => c.Column1)



                    };
            bSourceDetailCount.DataSource = a.ToList();
            dGridDetailCount.DataSource = bSourceDetailCount;
            tabControl1.SelectedIndex = 1;

        }
        //查询按钮
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                baseSet.Query q = new baseSet.Query(null, dhDataAcess.baseBLL.dataConn.connOpen());
                bSourceDetailMrp.DataSource = q.sqlQueryLikeAll(moduleInfo.V_orderDetailMrp, textbillNoQuery.Text.ToString().Trim());
                dGridDetaiMrp.DataSource = bSourceDetailMrp;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //刷新按钮
        private void buttupdate_Click(object sender, EventArgs e)
        {
            V_orderDetailMrpTableAdapter detailMrpAdapter = new V_orderDetailMrpTableAdapter();
            bSourceDetailMrp.DataSource = detailMrpAdapter.GetDataV_orderDetailMrp();

            dGridDetaiMrp.DataSource = bSourceDetailMrp;
            DcolumnSet.colSet(moduleInfo.V_orderDetailMrp, dGridDetaiMrp);
        }

        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //刷新按钮
        private void button3_Click(object sender, EventArgs e)
        {
            mrpCountTableAdapter mrpCountAdapter = new mrpCountTableAdapter();
            dhDataAcess.dataAcess.produceDataSet.mrpCountDataTable countTable = mrpCountAdapter.GetDatamrpCount("审核", "");
            bSourceMrpCount.DataSource = countTable;
            dGridMrpCount.DataSource = bSourceMrpCount;
            DcolumnSet.colSet(moduleInfo.mrpCount, dGridMrpCount);
            var a = from c in countTable
                    group c by new { c.matNum, c.stockQty1, c.rlckqty } into g
                    select new
                    {

                        原材料名 = g.Key.matNum,
                        原材料库存 = g.Key.stockQty1,
                        已领料 = g.Key.rlckqty,
                        未领料 = g.Sum(c => c.Column1) - g.Key.rlckqty,
                        所需罗数 = g.Sum(c => c.grCount),
                        所需码数 = g.Sum(c => c.yCount),
                        所需米数 = g.Sum(c => c.mCount),
                        百米前公斤 = g.Sum(c => c.Column2),
                        百米后公斤 = g.Sum(c => c.Column1)



                    };
            bSourceDetailCount.DataSource = a.ToList();
            dGridDetailCount.DataSource = bSourceDetailCount;
        }

        private void dGridMrpCount_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
           //// 对第1列相同单元格进行合并
           // if (e.ColumnIndex == 0 && e.RowIndex != -1)
           // {
           //     using
           //         (
           //         Brush gridBrush = new SolidBrush(this.dGridMrpCount.GridColor),
           //         backColorBrush = new SolidBrush(e.CellStyle.BackColor)
           //         )
           //     {
           //         using (Pen gridLinePen = new Pen(gridBrush))
           //         {
           //             // 清除单元格
           //             e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

           //             // 画 Grid 边线（仅画单元格的底边线和右边线）
           //             //   如果下一行和当前行的数据不同，则在当前的单元格画一条底边线
           //             if (e.RowIndex < dGridMrpCount.Rows.Count - 1 &&
           //             dGridMrpCount.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString())
           //                 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
           //                 e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
           //                 e.CellBounds.Bottom - 1);
           //             // 画右边线
           //             e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
           //                 e.CellBounds.Top, e.CellBounds.Right - 1,
           //                 e.CellBounds.Bottom);

           //             // 画（填写）单元格内容，相同的内容的单元格只填写第一个
           //             if (e.Value != null)
           //             {
           //                 if (e.RowIndex > 0 && dGridMrpCount.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString())
           //                 { }
           //                 else
           //                 {
           //                     e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
           //                         Brushes.Black, e.CellBounds.X + 2,
           //                         e.CellBounds.Y + 5, StringFormat.GenericDefault);
           //                 }
           //             }
           //             e.Handled = true;
           //         }
           //     }
           // }
        }

        private void 新增领料单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string qty = dGridDetaiMrp.CurrentRow.Cells["qty"].Value.ToString();
            string pname = dGridDetaiMrp.CurrentRow.Cells["pname"].Value.ToString();
            string unit = dGridDetaiMrp.CurrentRow.Cells["unit"].Value.ToString();
             string orderDetailNo = dGridDetaiMrp.CurrentRow.Cells["orderDetailNo"].Value.ToString();
            string qtyM=dGridDetaiMrp.CurrentRow.Cells["M"].Value.ToString();
            FrmproductblMasterAdd productbl = new FrmproductblMasterAdd(bSourceDetailMrp, "新增", pname, unit, orderDetailNo, qty, qtyM,"");
            productbl.ShowDialog();
            productbl.Owner = this;
        }

        private void toolQuery_Click(object sender, EventArgs e)
        {
            FrmscrlMasterAdd scrlMasterAdd = new FrmscrlMasterAdd(bSourceDetailMrp, "新增");
            scrlMasterAdd.ShowDialog();
            scrlMasterAdd.Owner = this;
        }

        private void dGridDetaiMrp_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            decimal count = 0;
            for (int i = 0; i < dGridDetaiMrp.SelectedRows.Count; i++)
            {
                if (dGridDetaiMrp.SelectedRows[i].Cells[dGridDetaiMrp.CurrentCell.ColumnIndex].Value != null)
                {
                    try
                    {
                        count = count + decimal.Parse(dGridDetaiMrp.SelectedRows[i].Cells[dGridDetaiMrp.CurrentCell.ColumnIndex].Value.ToString());
                    }
                    catch (Exception)
                    {
                        continue;//非数字时,会报错,跳过
                    }
                }
            }
            if (count == 0)
            {
                this.textBox1.Text = "";
            }
            else
            {
                this.textBox1.Text = "求和=" + count.ToString();
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //string qty = dGridDetaiMrp.CurrentRow.Cells["qty"].Value.ToString();
            string pname = dGridMRP.CurrentRow.Cells["pname"].Value.ToString();
            string unit ="M";
            //string orderDetailNo = dGridDetaiMrp.CurrentRow.Cells["orderDetailNo"].Value.ToString();
            string qtyM = dGridMRP.CurrentRow.Cells["M"].Value.ToString();
            FrmproductblMasterAdd productbl = new FrmproductblMasterAdd(bSourceDetailMrp, "新增", pname, unit, "", "", qtyM, "");
            productbl.ShowDialog();
            productbl.Owner = this;
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.V_Mrp, dGridMRP, "生产备料信息");
            frm.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                bgSourceMRP.DataSource = productbl.getV_MrpAdapterAllbylikepname ("%" + textBox3.Text + "%");
                dGridMRP.DataSource = bgSourceMRP;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

           
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            //已审核订单
            if (e.TabPage == tabPage1)
            {
                V_orderDetailMrpTableAdapter detailMrpAdapter = new V_orderDetailMrpTableAdapter();
                bSourceDetailMrp.DataSource = detailMrpAdapter.GetDataV_orderDetailMrp();

                dGridDetaiMrp.DataSource = bSourceDetailMrp;
                DcolumnSet.colSet(moduleInfo.V_orderDetailMrp, dGridDetaiMrp);
            }
            //未审核订单
            if (e.TabPage == tabPage2)
            {
                mrpCountTableAdapter mrpCountAdapter = new mrpCountTableAdapter();
                produceDataSet.mrpCountDataTable countTable = mrpCountAdapter.GetDatamrpCount("审核", "");
                purchase.rlckDetailDataTable rlckTable = new purchase.rlckDetailDataTable();
                bSourceMrpCount.DataSource = countTable;
                dGridMrpCount.DataSource = bSourceMrpCount;
                DcolumnSet.colSet(moduleInfo.mrpCount, dGridMrpCount);
                var a = from c in countTable
                        group c by new { c.matNum, c.rlckqty } into g
                        select new
                        {

                            原材料名 = g.Key.matNum,
                            //  原材料库存 = g.Key.stockQty1,
                            已领料 = g.Key.rlckqty,
                            未领料 = g.Sum(c => c.Column1) - g.Key.rlckqty,
                            所需罗数 = g.Sum(c => c.grCount),
                            所需码数 = g.Sum(c => c.yCount),
                            所需米数 = g.Sum(c => c.mCount),
                            百米前公斤 = g.Sum(c => c.Column2),
                            百米后公斤 = g.Sum(c => c.Column1)
                        };
                bSourceDetailCount.DataSource = a.ToList();
                dGridDetailCount.DataSource = bSourceDetailCount;
                dGridDetaiMrp.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dGridDetaiMrp.CellMouseUp += new DataGridViewCellMouseEventHandler(dGridDetaiMrp_CellMouseUp);
                this.dGridDetaiMrp.EditMode = DataGridViewEditMode.EditOnEnter;
            }
            //未审核订单
            if (e.TabPage == tabPage3)
            {
                V_MrpTableAdapter V_MrpAdapter = new V_MrpTableAdapter();
                bgSourceMRP.DataSource = V_MrpAdapter.GetDataV_MrpALL();

                dGridMRP.DataSource = bgSourceMRP;
                DcolumnSet.colSet(moduleInfo.V_Mrp, dGridMRP);
            }
        }

        private void buttprint_Click(object sender, EventArgs e)
        {
            //string orderNo = dGridorderproduce.CurrentRow.Cells["orderNo"].Value.ToString();
            //string ptype = dGridorderMaster.CurrentRow.Cells["ptype"].Value.ToString();
            Report r = new Report();
            DataSet ds = new DataSet();
            V_MrpTableAdapter V_MrpAdapter = new V_MrpTableAdapter();
            DataTable dt = V_MrpAdapter.GetDataV_MrpALL();
            
            dt.Columns.Add("custname");
            foreach (DataRow row in dt.Rows)
            {
               
             DataTable s=  basicSql.HeReportDetail(row["pname"].ToString());
             string mark = null;
             foreach (DataRow a in s.Rows)
             {
                 mark = mark + a["mark"].ToString() + "  ";
             }
             row["custname"] = mark;
            }
            ds.Tables.Add(dt);
          // r.Load(@"E:\东华软件项目\报表\pnameproduct.frx");
            r.Load(@"\\192.168.0.27\东华软件\报表\pnameproduct.frx");
            r.RegisterData(ds);
          //  r.Design();
            r.Show();
        }
    }
}
